﻿using Microsoft.AspNetCore.Mvc;
using RBAC_Application.Dto.Input;
using RBAC_Application.Dto.Output;
using RBAC_Application.Dto.批量分配;
using RBAC_Application.Repository.Interface;
using RBAC_Application.Service.RBAC;
using Standard;

namespace B.MES.Login.Controllers
{
    /// <summary>
    /// 用户角色管理
    /// </summary>
    [Route("api/[controller]")]
    [ApiController]
    public class UserRoleController : ControllerBase
    {
        private readonly IRbacService _rbacService;

        public UserRoleController(IRbacService rbacService)
        {
            _rbacService = rbacService;
        }

        /// <summary>
        /// 给用户分配角色
        /// </summary>
        [HttpGet("assign")]
        public ApiResult<int> AssignUserRole([FromQuery] AssignUserRoleInput  input)
        {
            try
            {
                _rbacService.AssignUserRole(input);
                return new ApiResult<int>
                {
                    Code = ApiCode.Success,
                    ResultData = 1,
                    Message = "分配成功"
                };
            }
            catch (Exception ex)
            {
                return new ApiResult<int>
                {
                    Code = ApiCode.InternalError,
                    Message = ex.Message
                };
            }
        }

        /// <summary>
        /// 移除用户的角色
        /// </summary>
        [HttpPost("remove")]
        public ApiResult<int> RemoveUserRole([FromQuery] long userId, [FromQuery] long roleId)
        {
            try
            {
                _rbacService.RemoveUserRole(userId, roleId);
                return new ApiResult<int>
                {
                    Code = ApiCode.Success,
                    ResultData = 1,
                    Message = "移除成功"
                };
            }
            catch (Exception ex)
            {
                return new ApiResult<int>
                {
                    Code = ApiCode.InternalError,
                    Message = ex.Message
                };
            }
        }

        /// <summary>
        /// 查询用户拥有的角色
        /// </summary>
        [HttpGet("user-roles/{userId}")]
        public ApiResult<List<RoleDto>> GetUserRoles(long userId)
        {
            try
            {
                var roles = _rbacService.GetUserRoles(userId);
                return new ApiResult<List<RoleDto>>
                {
                    Code = ApiCode.Success,
                    ResultData = roles,
                    Message = "获取成功"
                };
            }
            catch (Exception ex)
            {
                return new ApiResult<List<RoleDto>>
                {
                    Code = ApiCode.InternalError,
                    Message = ex.Message
                };
            }
        }

        /// <summary>
        /// 查询角色下的用户
        /// </summary>
        [HttpGet("role-users/{roleId}")]
        public ApiResult<List<UserDto>> GetRoleUsers(long roleId)
        {
            try
            {
                var users = _rbacService.GetRoleUsers(roleId);
                return new ApiResult<List<UserDto>>
                {
                    Code = ApiCode.Success,
                    ResultData = users,
                    Message = "获取成功"
                };
            }
            catch (Exception ex)
            {
                return new ApiResult<List<UserDto>>
                {
                    Code = ApiCode.InternalError,
                    Message = ex.Message
                };
            }
        }
        /// <summary>
        /// 批量分配用户角色
        /// </summary>
        /// <param name="userId">id</param>
        /// <param name="roleIds">批量id</param>
        [HttpPost("assign-batch")]
        public ApiResult<int> AssignUserRolesBatch([FromBody] AssignUserRolesBatchInput input)
        {
            try
            {
                _rbacService.AssignUserRolesBatch(input);
                return new ApiResult<int> { Code = ApiCode.Success, ResultData = 1, Message = "批量分配成功" };
            }
            catch (Exception ex)
            {
                return new ApiResult<int> { Code = ApiCode.InternalError, Message = ex.Message };
            }
        }
        /// <summary>
        /// 批量删除用户角色
        /// </summary>
        /// <param name="userId">id</param>
        /// <param name="roleIds">批量id</param>
        [HttpPost("remove-batch")]
        public ApiResult<int> RemoveUserRolesBatch([FromBody] UserRoleBatchInput input)
        {
            try
            {
                _rbacService.RemoveUserRolesBatch(input.UserId, input.RoleIds);
                return new ApiResult<int> { Code = ApiCode.Success, ResultData = 1, Message = "批量移除成功" };
            }
            catch (Exception ex)
            {
                return new ApiResult<int> { Code = ApiCode.InternalError, Message = ex.Message };
            }
        }
    }
}